package palmtop.com.palmtop.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.AwesomeTextView;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.model.Reportpopulation;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.FuncUtil;
import palmtop.com.palmtop.util.NetUtil;
import palmtop.com.palmtop.util.PlaybackDialogFragment;
import palmtop.com.palmtop.util.RecordingItem;
import palmtop.com.palmtop.util.ToastUtil;

public class PopulationdetailActivity extends BaseActivity {

    @BindView(R.id.mtitle)
    TextView mtitle;
    @BindView(R.id.reportman)
    AwesomeTextView reportman;
    @BindView(R.id.position)
    AwesomeTextView position;
    @BindView(R.id.palcetypename)
    AwesomeTextView palcetypename;
    @BindView(R.id.addtime)
    AwesomeTextView addtime;
    @BindView(R.id.homeowner)
    AwesomeTextView homeowner;
    @BindView(R.id.homeownerID)
    AwesomeTextView homeownerID;
    @BindView(R.id.lessee)
    AwesomeTextView lessee;
    @BindView(R.id.lesseeID)
    AwesomeTextView lesseeID;


    Reportpopulation reportpopulation;
    @BindView(R.id.zphoto1)
    ImageView zphoto1;
    @BindView(R.id.zphoto2)
    ImageView zphoto2;
    @BindView(R.id.zphoto3)
    ImageView zphoto3;
    @BindView(R.id.zphoto4)
    ImageView zphoto4;
    @BindView(R.id.confirm)
    Button confirm;
    @BindView(R.id.reject)
    Button reject;
    @BindView(R.id.blay)
    LinearLayout blay;
    @BindView(R.id.v1)
    TextView v1;
    @BindView(R.id.content)
    AwesomeTextView content;
    @BindView(R.id.lay1)
    LinearLayout lay1;
    @BindView(R.id.v2)
    TextView v2;
    @BindView(R.id.lay2)
    LinearLayout lay2;
    @BindView(R.id.v3)
    TextView v3;
    @BindView(R.id.lay3)
    LinearLayout lay3;
    @BindView(R.id.v4)
    TextView v4;
    @BindView(R.id.lay4)
    LinearLayout lay4;
    @BindView(R.id.v5)
    TextView v5;
    @BindView(R.id.lay5)
    LinearLayout lay5;
    @BindView(R.id.v6)
    TextView v6;
    @BindView(R.id.tname)
    AwesomeTextView tname;
    @BindView(R.id.lay6)
    LinearLayout lay6;
    @BindView(R.id.v7)
    TextView v7;
    @BindView(R.id.tId)
    AwesomeTextView tId;
    @BindView(R.id.lay7)
    LinearLayout lay7;
    @BindView(R.id.v8)
    TextView v8;
    @BindView(R.id.tphone)
    AwesomeTextView tphone;
    @BindView(R.id.lay8)
    LinearLayout lay8;
    @BindView(R.id.v9)
    TextView v9;
    @BindView(R.id.employsituation)
    AwesomeTextView employsituation;
    @BindView(R.id.lay9)
    LinearLayout lay9;
    @BindView(R.id.v10)
    TextView v10;
    @BindView(R.id.taddress)
    AwesomeTextView taddress;
    @BindView(R.id.lay10)
    LinearLayout lay10;
    @BindView(R.id.v11)
    TextView v11;
    @BindView(R.id.status)
    AwesomeTextView status;
    @BindView(R.id.lay11)
    LinearLayout lay11;
    @BindView(R.id.play)
    Button play;
    @BindView(R.id.lookmapBtn)
    Button lookmapBtn;
    @BindView(R.id.v21)
    TextView v21;
    @BindView(R.id.lesseeaddress)
    AwesomeTextView lesseeaddress;
    @BindView(R.id.lay21)
    LinearLayout lay21;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_populationdetail2, Config.toptitle, 1);
        MyApplication app = (MyApplication) getApplication();
        app.addActivityList(PopulationdetailActivity.this);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mtitle.setText("信息详情");
        AppPreferences appPreferences = new AppPreferences(PopulationdetailActivity.this);
        String role_id = appPreferences.getString("role_id", "");
        /*if (role_id.equals("73006b03b7f447bf953d065462e5b1ac") || role_id.equals("1f515845ed214961aef8a0bc6646cf2d")) {
            blay.setVisibility(View.GONE);
        }*/

        reportpopulation = (Reportpopulation) getIntent().getSerializableExtra("reportpopulation");

        if (reportpopulation != null) {
            if (role_id.equals("73006b03b7f447bf953d065462e5b1ac")) {
                FuncUtil.updateReadPopulation(String.valueOf(reportpopulation.getId()));
            }

            if (role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5") && reportpopulation.getStatus().equals("0")) {

            } else {
                blay.setVisibility(View.GONE);
                v11.setVisibility(View.VISIBLE);
                lay11.setVisibility(View.VISIBLE);
            }
            if (TextUtils.isEmpty(reportpopulation.getRecordurl())) {
                play.setEnabled(false);
            }
//            if (role_id.equals("73006b03b7f447bf953d065462e5b1ac")) {
            if (reportpopulation.getStatus().equals("0")) {
                status.setText("待确认");
            }
            if (reportpopulation.getStatus().equals("1")) {
                status.setText("已采纳");
            }
            if (reportpopulation.getStatus().equals("2")) {
                status.setText("未采纳");
            }
//            }
            reportman.setText(reportpopulation.getReportman());
            position.setText(reportpopulation.getPosition());
            palcetypename.setText(reportpopulation.getPalcetypename());
            addtime.setText(reportpopulation.getAddtime());
            if (reportpopulation.getPalcetypename().equals("九小场所")) {
                v1.setVisibility(View.VISIBLE);
                lay1.setVisibility(View.VISIBLE);
                content.setText(reportpopulation.getContent());
            }
            if (reportpopulation.getPalcetypename().equals("租赁房屋")) {
                v2.setVisibility(View.VISIBLE);
                lay2.setVisibility(View.VISIBLE);

                v3.setVisibility(View.VISIBLE);
                lay3.setVisibility(View.VISIBLE);

                v4.setVisibility(View.VISIBLE);
                lay4.setVisibility(View.VISIBLE);

                v5.setVisibility(View.VISIBLE);
                lay5.setVisibility(View.VISIBLE);
                v21.setVisibility(View.VISIBLE);
                lay21.setVisibility(View.VISIBLE);


                homeowner.setText(reportpopulation.getHomeowner());
                homeownerID.setText(reportpopulation.getHomeownerid());
                lessee.setText(reportpopulation.getLessee());
                lesseeID.setText(reportpopulation.getLesseeid());
                lesseeaddress.setText(reportpopulation.getLeaseaddress());

            }
            if (reportpopulation.getPalcetypename().equals("人口")) {
                v6.setVisibility(View.VISIBLE);
                lay6.setVisibility(View.VISIBLE);

                v7.setVisibility(View.VISIBLE);
                lay7.setVisibility(View.VISIBLE);

                v8.setVisibility(View.VISIBLE);
                lay8.setVisibility(View.VISIBLE);

                v9.setVisibility(View.VISIBLE);
                lay9.setVisibility(View.VISIBLE);

                v10.setVisibility(View.VISIBLE);
                lay10.setVisibility(View.VISIBLE);

                tname.setText(reportpopulation.getTname());
                tId.setText(reportpopulation.gettId());
                tphone.setText(reportpopulation.getTphone());
                employsituation.setText(reportpopulation.getEmploysituation());
                taddress.setText(reportpopulation.getTaddress());
            }


            if (reportpopulation.getPhoto1() != null && !reportpopulation.getPhoto1().equals("")) {
                x.image().bind(zphoto1, Config.IMG_URL + reportpopulation.getPhoto1());
                FuncUtil.clickShowBigPic(Config.IMG_URL + reportpopulation.getPhoto1(), zphoto1, PopulationdetailActivity.this);
            }
            if (reportpopulation.getPhoto2() != null && !reportpopulation.getPhoto2().equals("")) {
                x.image().bind(zphoto2, Config.IMG_URL + reportpopulation.getPhoto2());
                FuncUtil.clickShowBigPic(Config.IMG_URL + reportpopulation.getPhoto2(), zphoto2, PopulationdetailActivity.this);
            }
            if (reportpopulation.getPhoto3() != null && !reportpopulation.getPhoto3().equals("")) {
                x.image().bind(zphoto3, Config.IMG_URL + reportpopulation.getPhoto3());
                FuncUtil.clickShowBigPic(Config.IMG_URL + reportpopulation.getPhoto3(), zphoto3, PopulationdetailActivity.this);
            }
            if (reportpopulation.getPhoto4() != null && !reportpopulation.getPhoto4().equals("")) {
                x.image().bind(zphoto4, Config.IMG_URL + reportpopulation.getPhoto4());
                FuncUtil.clickShowBigPic(Config.IMG_URL + reportpopulation.getPhoto4(), zphoto4, PopulationdetailActivity.this);
            }
        }

    }

    @OnClick(R.id.confirm)
    public void onConfirmClicked() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "reportpopulation/updateStatus");
        params.addBodyParameter("id", String.valueOf(reportpopulation.getId()));
        params.addBodyParameter("status", "1");
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("success")) {
                    ToastUtil.show(PopulationdetailActivity.this, "确认成功");
                    finish();

                } else {
                    ToastUtil.show(PopulationdetailActivity.this, "确认失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    @OnClick(R.id.reject)
    public void onRejectClicked() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "reportpopulation/updateStatus");
        params.addBodyParameter("id", String.valueOf(reportpopulation.getId()));
        params.addBodyParameter("status", "2");
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("success")) {
                    ToastUtil.show(PopulationdetailActivity.this, "驳回成功");
                    finish();

                } else {
                    ToastUtil.show(PopulationdetailActivity.this, "驳回失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    @OnClick(R.id.play)
    public void onViewClicked() {
        RecordingItem recordingItem = new RecordingItem();
        SharedPreferences sharePreferences = getSharedPreferences("sp_name_audio", MODE_PRIVATE);
        String filePath = Config.base_url + reportpopulation.getRecordurl();
        int elpased = Integer.valueOf(reportpopulation.getRecordlength());
        recordingItem.setFilePath(filePath);
        recordingItem.setLength(elpased);
//        recordingItem.setLength((int)6);
        PlaybackDialogFragment fragmentPlay = PlaybackDialogFragment.newInstance(recordingItem);
        fragmentPlay.show(getSupportFragmentManager(), PlaybackDialogFragment.class.getSimpleName());
    }

    @OnClick(R.id.lookmapBtn)
    public void onlookmapClicked() {
        Intent i = new Intent();
        if (!TextUtils.isEmpty(reportpopulation.getLatitude())) {
            i.putExtra("latitude", reportpopulation.getLatitude());
        }
        if (!TextUtils.isEmpty(reportpopulation.getLongitude())) {
            i.putExtra("longitude", reportpopulation.getLongitude());
        }
        i.setClass(this, LookmapActivity.class);
        startActivityForResult(i, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
